//
//  COCOAFANViewController.m
//  CocoaFan
//
//  Created by Chen Xianan on 2009-02-25.
//  Copyright 2009 realazy.org All rights reserved.
//

#import "COCOAFANViewController.h"
#import "COCOAFANBubbleView.h"
#import "COCOAFANTableView.h"

NSString * const COCOAFANReplyNotification = @"replyNotification";
NSString * const COCOAFANForwardNotification = @"forwardNotification";
NSString * const COCOAFANPmsgNotification = @"pmsgNotification";
NSString * const COCOAFANDeleteNotification = @"deleteNotification";

@implementation COCOAFANViewController

@synthesize statusContent, selected, bubbleView, statusField;

- (id)init
{
	if (![super initWithNibName:@"StatusItemView"
						 bundle:nil])
		return nil;

	return self;
}

- (void)awakeFromNib
{
	[[self bubbleView] setStatusType:[[[self statusContent] objectForKey:@"type"] intValue]];
}

- (void)dealloc
{
	[self setStatusContent:nil];
	[super dealloc];
}

#pragma mark IBActions

- (IBAction)reply:(id)sender
{
	[[NSNotificationCenter defaultCenter]
		postNotificationName:COCOAFANReplyNotification
					  object:self
					userInfo:[self statusContent]];
}

- (IBAction)pmsg:(id)sender
{
	[[NSNotificationCenter defaultCenter]
		postNotificationName:COCOAFANPmsgNotification
					  object:self
					userInfo:[self statusContent]];
}

- (IBAction)forward:(id)sender
{
	[[NSNotificationCenter defaultCenter]
		postNotificationName:COCOAFANForwardNotification
					  object:self
					userInfo:[self statusContent]];
}

- (IBAction)delete:(id)sender
{
	[[NSNotificationCenter defaultCenter]
		postNotificationName:COCOAFANDeleteNotification
					  object:self
					userInfo:[self statusContent]];
}

- (IBAction)openMessageInBrowser:(id)sender
{
	[AppController openMessageInBrowser:statusContent];
}

- (IBAction)openProfileInBrowser:(id)sender
{
	[AppController openProfileInBrowser:statusContent];
}

#pragma mark NSTextField Delegate

- (BOOL)control:(NSControl *)control
	   textView:(NSTextView *)textView
doCommandBySelector:(SEL)command
{
	if (command == @selector(moveDown:) || command == @selector(moveUp:)){
		COCOAFANTableView *table = (COCOAFANTableView *)[[self view] superview];
		BOOL jump = NO;
		if (command == @selector(moveDown:)){
			[table selectNextRow];
			jump = YES;
		} else {
			[table selectPreviousRow];
			jump = NO;
		}
		if (jump) [[[self view] window] makeFirstResponder:table];
		return NO;
	} 		
	return YES;
}

@end
